package com.gm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gm.bean.Admin;
import com.gm.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author Administrator
 * @Date 2020/6/28 14:24
 **/
@Controller
public class
AdminController {
    @Autowired
    AdminMapper adminMapper;
    @GetMapping("/admins")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "5") Integer size,
                          Model model){
        PageHelper.startPage(page,size);
        List<Admin> adminList = this.adminMapper.findAdminAll(page,size);
        PageInfo<Admin> pageInfo = new PageInfo<>(adminList);
        model.addAttribute("admins",pageInfo);
        return "admin-list";
    }
    @PostMapping("/admin")
    public String adminAdd(Admin admin){
        admin.setCreateTime(new Date());
        admin.setUpdateTime(new Date());
        this.adminMapper.addAdmin(admin);
        return "redirect:/admins";
    }
    @DeleteMapping("/admin/{id}")
    public String delAdmin(@PathVariable("id") Integer id){
        this.adminMapper.delAdmin(id);
        return "redirect:/admins";
    }
    @GetMapping("/admin/{nickname}")
    public String adminUpd(@PathVariable("nickname") String nickname, Model model){
        Admin admin = this.adminMapper.getAdminByName(nickname);
        model.addAttribute("select",admin);
        return "admin-upd";
    }
    @PutMapping("/admin")
    public String adminUpd(Admin admin){
        this.adminMapper.adminUpd(admin);
        return "redirect:/admins";
    }

}
